<template>
  <div>
   <Header></Header>
    <div class="tag-group">
    <span class="tag-group__title">分类</span>
      &nbsp;
      <el-tag
          key="全部"
          type="info"
          effect="plain">
        <el-link @click="changeClassification('')"  type="primary" >全部</el-link>
      </el-tag>
    <el-tag
        v-for="classification in classifications"
        :key="classification.name"
        type="info"
        effect="plain">
      <el-link @click="changeClassification(classification.id)"  type="primary" >{{classification.name}}</el-link>
    </el-tag>
    </div>

    <br>
    <div class="tag-group">
      <span class="tag-group__title">年份</span>
      &nbsp;
      <el-tag
          key="全部"
          type="info"
          effect="plain">
        <el-link @click="changeYear('')"  type="primary" >全部</el-link>
      </el-tag>
      <el-tag
          v-for="year in years"
          :key="year.name"
          type="info"
          effect="plain">
        <el-link @click="changeYear(year.id)"  type="primary" >{{year.name}}</el-link>
      </el-tag>
    </div>
<!--    <el-link  type="info" v-for="film in films">{{film.name}}</el-link>-->
    <el-row  v-loading="loading"
             element-loading-text="拼命加载中"
             element-loading-spinner="el-icon-loading"
             element-loading-background="rgba(0, 0, 0, 0.8)">
      <el-col :span="8" v-for="film in films"  >
        <el-card :body-style="{ padding: '0px' }">
          <img :src="film.pic" class="image">
          <div style="padding: 14px;">
            <span>{{film.name}}</span>
            <div class="bottom clearfix">
              <time class="time">{{film.createdTime}}</time>
              <el-button type="text" class="button" @click="collection(film.id)">收藏</el-button>
              <el-button type="text" class="button" @click="filmDetail(film.id)">详情</el-button>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <br>
    <el-pagination class="mpage"
                   background
                   layout="prev, pager, next"
                   :current-page="currentPage"
                   :page-size="pageSize"
                   :total="total"
                   @current-change=getFilms>
    </el-pagination>
  </div>
</template>
<script>
import Header from "../components/Header";
export default {
  name: "Index.vue",
  components: {Header},
  data() {
    return {
      classifications: {},
      years:{},
      films:{},
      classification:'',
      year:'',
      currentPage: 1,
      total: 0,
      pageSize: 5,
      collectionForm:{
        id:'',
        filmId:'',
        CreatedUserId:''
      }
    }
  },
  methods: {
    getClassifications(){
      const _this = this
      _this.$axios.get("/classification/list").then(res => {
        _this.classifications = res.data.data
      })
    },
    getYears(){
      const _this = this
      _this.$axios.get("/year/list").then(res => {
        _this.years = res.data.data
      })
    },
    changeYear(id){
      this.year = id
    },
    changeClassification(id){
      this.classification =id
    },
    page(currentPage){
      const _this = this
      _this.$axios.get("/film/page").then(res => {
        _this.films = res.data.data.records
        _this.currentPage = res.data.data.current
        _this.total = res.data.data.total
        _this.pageSize = res.data.data.size
      })
    },
    getFilms(currentPage){
      const _this = this
      _this.$axios.get("/film/page?currentPage="+currentPage+"&classId="+this.classification+"&yearId="+this.year).then(res => {
        _this.films = res.data.data.records
        _this.currentPage = res.data.data.current
        _this.total = res.data.data.total
        _this.pageSize = res.data.data.size
      })
    },
    filmDetail(id){
      this.$router.push("/film/"+id)
    },
    collection(id){
      const _this = this
      this.collectionForm.filmId=id
      this.collectionForm.id=''
      this.collectionForm.createdUserId=this.$store.getters.getUser.id
      _this.$axios.post("/collection/add",this.collectionForm).then(res => {
          if(res.data.data===true){
            this.$message({
              showClose: true,
              message: res.data.message,
              type: 'success'
            });
          }else{
            this.$message.error("收藏失败");
          }
      })
    }

  },
  created() {
    this.getClassifications()
    this.getYears()
    this.getFilms(this.currentPage,'','')
  },
  watch:{
    year(val){
      this.getFilms(1,this.classification,this.year)
    },
    classification(val){
      this.getFilms(1,this.classification,this.year)
    }
  }
}
</script>
<style scoped>
.mpage {
  margin: 0 auto;
  text-align: center;
}
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 50%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>